package day19.feifei;

public class Demo01 {

        public static void main(String[] args) {
            // 创建共享的账户对象数据
            Account account = new Account("001", 1000);
            // Account account2 = new Account("002",2000);
            // 创建两个用户线程
            UserThread t1 = new UserThread(account, "用户A");
            t1.setMoney(500);
            UserThread t2 = new UserThread(account, "用户B");
            t2.setMoney(500);
            // 开启两个分线程
            t1.start();
            t2.start();

        }
    }

    // 定义一个用户线程类
    class UserThread extends Thread {
        // 共用同一个账户
        private /*static*/ Account account;// 静态成员变量
        // 设置取钱的金额
        private double money;

        // 重写run方法
        @Override
        public void run() {
            account.withdraw(money);
            System.out.println(Thread.currentThread().getName() + "取钱后的余额为：" + account.getBalance());
        }

        public UserThread(Account account, String name) {
            super(name);
            this.account = account;
        }

        public Account getAccount() {
            return account;
        }

        public void setAccount(Account account) {
            this.account = account;
        }

        public double getMoney() {
            return money;
        }

        public void setMoney(double money) {
            this.money = money;
        }
    }

    // 定义一个账户类
    class Account {
        // 卡号
        private String acc_ID;
        // 余额
        private double balance;
        final Object obj  = new Object();// obj

        // 取钱的功能
        public void withdraw(double money) {
            System.out.println(Thread.currentThread().getName() + ",余额为：" + balance);
            // 此时balance数据它是一个多线程当中共享的数据
            // 锁是有状态的 默认情况下锁的状态值为1 1代表可以拥有当前这把锁
            // 如果锁的状态的为0，0代表当前的这把锁已经被其他线程拿走了
            // 此时其他线程就停在这个位置
            // this---> t1
            // this --->t2
            // synchronized (this) {  this---->任意对象  ?
            // synchronized(锁对象) 锁对象指代的是一把共享锁
            //Object obj  = new Object();// obj
            synchronized (this) {//
                System.out.println(this);
                if (money <= balance) {
                    double before = balance;
                    // 取钱后
                    double after = balance - money;
                    // 发起取钱的动作 sleep(2000)
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    balance = after;// 余额才会减少
                } else {
                    System.out.println("余额不足！");
                }
            }
        }

        @Override
        public String toString(){
            return"Account{}";
        }

        public Account(){
        }

        public Account(String acc_ID,double balance){
            this.acc_ID=acc_ID;
            this.balance=balance;
        }

        public String getAcc_ID(){
            return acc_ID;
        }

        public void setAcc_ID(String acc_ID){
            this.acc_ID=acc_ID;
        }

        public double getBalance(){
            return balance;
        }

        public void setBalance(double balance){
            this.balance=balance;
        }
    }
